package nii.game.mobileserio.action_game;

import nii.game.mobileserio.database.Database;
import nii.game.mobileserio.dialog.DialogExit;
import nii.game.mobileserio.dialog.DialogViewScore;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.SQLException;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class Luudiem extends Activity implements  OnClickListener{

	//======================|| Declarations ||===========================//
	
	Button menu, choilai, luu, xem_diem_cao;
	TextView text_diem;
	EditText edit_ten;
	
	private Database db;
	int diem = 0;
	
	//====================|| onCreate Function ||========================//
	
	public void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		
		setContentView(R.layout.luudiem);
		
		db = new Database(this);
		
		Bundle b = getIntent().getExtras();
		
		if(b != null){
			diem = b.getInt("diem");
		}
		
		//nút Lưu điểm ------------------------------------
		luu = (Button) findViewById(R.id.luudiem);
		luu.setOnClickListener(this);
		
		//nút Menu -----------------------------------------
		menu = (Button) findViewById(R.id.menu);
		menu.setOnClickListener(this);
		
		//nút Xem điểm --------------------------------------
		xem_diem_cao = (Button) findViewById(R.id.xemdiem);
		xem_diem_cao.setOnClickListener(this);
		
		//TextView điểm ------------------------------------
		text_diem = (TextView) findViewById(R.id.diem);
		text_diem.setText(String.valueOf(diem));
		
		// Edit text-----------------------------------------
		edit_ten = (EditText) findViewById(R.id.name);
	}
	
	// onClick Function
	public void onClick(View arg0) {
		switch(arg0.getId())
		{
			case R.id.luudiem:
				LuuDiem();
				break;
				
			case R.id.menu:
				Intent i = new Intent(this, MenuActivity.class);
				this.startActivity(i);
				this.finish();
				break;
				
			case R.id.xemdiem:
				XemDiem();
				break;
		}
	}

	// LuuDiem Function
	public void LuuDiem(){
		String name = edit_ten.getText().toString();
		int diem = Integer.parseInt(text_diem.getText().toString());
		
		// Nếu chưa nhập tên thì yêu cầu nhập tên
		if (name.length() == 0 || name == null){
			Toast.makeText(this, "Bạn chưa nhập tên!", Toast.LENGTH_LONG).show();
			return;
		}
		try{
			db.open();
			Cursor c = db.getAllRows();
			if (c.moveToPosition(9)){
				String id = c.getString(c.getColumnIndex(db.KEY_ROWID));
				int math = c.getInt(c.getColumnIndex(db.KEY_DIEM));
				if (math < diem){
					db.updatePlayer(id, name, diem);
					LinearLayout l = (LinearLayout)findViewById(R.id.linearLayout_diem);
					l.setVisibility(View.GONE);
					luu.setVisibility(View.GONE);
					xem_diem_cao.setVisibility(View.VISIBLE);
					XemDiem();
				}
			}
			c.close();
			db.close();
			
		}
		catch(SQLException e){
			e.printStackTrace();
		}
	}

	// XemDiem Function
	public void XemDiem(){
		DialogViewScore xemdiem = new DialogViewScore(this);
		xemdiem.show();
	}

	// onPause Function
	protected void onPause() {
		// TODO Auto-generated method stub
		this.finish();
		super.onPause();
	}
	
	// onDestroy Function
	protected void onDestroy() {
		super.onDestroy();
		this.finish();
	};

	// nút Back trên bàn phím
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_BACK){
			DialogExit dialogexit = new DialogExit(this,this);
			dialogexit.show();
		}
		return super.onKeyDown(keyCode, event);
	}
	
}
